/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.social.connect;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "connection_data")
public class ConnectionData implements Serializable {

	private static final long serialVersionUID = -3450047691505359243L;
	private ConnectionDataId id;
	private String displayName;
	private String profileUrl;
	private String imageUrl;
	private String accessToken;
	private String secret;
	private String refreshToken;
	private Long expireTime;
	private Integer rank;

	public ConnectionData(){}
	
	public ConnectionData(String providerId, String providerUserId,
			String userId, String displayName, String profileUrl,
			String imageUrl, String accessToken, String secret,
			String refreshToken, Long expireTime) {
		this.id.setProviderId(providerId);
		this.id.setProviderUserId(providerUserId);
		this.id.setUserId(userId);
		this.displayName = displayName;
		this.profileUrl = profileUrl;
		this.imageUrl = imageUrl;
		this.accessToken = accessToken;
		this.secret = secret;
		this.refreshToken = refreshToken;
		this.expireTime = expireTime;
	}

	public ConnectionData(String providerId, String providerUserId,
			String displayName, String profileUrl, String imageUrl,
			String accessToken, String secret, String refreshToken,
			Long expireTime) {
		this.id.setProviderId(providerId);
		this.id.setProviderUserId(providerUserId);
		this.displayName = displayName;
		this.profileUrl = profileUrl;
		this.imageUrl = imageUrl;
		this.accessToken = accessToken;
		this.secret = secret;
		this.refreshToken = refreshToken;
		this.expireTime = expireTime;
	}

	@Id
	public ConnectionDataId getId() {
		return id;
	}

	public void setId(ConnectionDataId id) {
		this.id = id;
	}

	/**
	 * A display name for the connection.
	 */
	@Column(name = "display_name")
	public String getDisplayName() {
		return displayName;
	}

	/**
	 * A link to the provider's user profile page.
	 */
	@Column(name = "profile_url")
	public String getProfileUrl() {
		return profileUrl;
	}

	/**
	 * An image visualizing the connection.
	 */
	@Column(name = "image_url")
	public String getImageUrl() {
		return imageUrl;
	}

	/**
	 * The access token required to make authorized API calls.
	 */
	@Column(name = "access_token")
	public String getAccessToken() {
		return accessToken;
	}

	/**
	 * The secret token needed to make authorized API calls. Required for
	 * OAuth1-based connections.
	 */
	@Column(name = "secret")
	public String getSecret() {
		return secret;
	}

	/**
	 * A token use to renew this connection. Optional. Always null for
	 * OAuth1-based connections.
	 */
	@Column(name = "refresh_token")
	public String getRefreshToken() {
		return refreshToken;
	}

	/**
	 * The time the connection expires. Optional. Always null for OAuth1-based
	 * connections.
	 */
	@Column(name = "expire_time")
	public Long getExpireTime() {
		return expireTime;
	}

	@Column(name = "rank")
	public Integer getRank() {
		return rank;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public void setProfileUrl(String profileUrl) {
		this.profileUrl = profileUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public void setRefreshToken(String refreshToken) {
		this.refreshToken = refreshToken;
	}

	public void setExpireTime(Long expireTime) {
		this.expireTime = expireTime;
	}

	public void setRank(Integer rank) {
		this.rank = rank;
	}

	@Transient
	public String getProviderId() {
		return id.getProviderId();
	}

	@Transient
	public String getProviderUserId() {
		return id.getProviderUserId();
	}
	
	@Transient
	public String getUserId() {
		return id.getUserId();
	}

	@Embeddable
	public class ConnectionDataId implements Serializable {
		private static final long serialVersionUID = -6276975789801195372L;
		private String userId;
		private String providerId;
		private String providerUserId;
	
		/**
		 * The id of the provider the connection is associated with.
		 */
		@Column(name = "provider_id")
		public String getProviderId() {
			return providerId;
		}

		/**
		 * The id of the provider user this connection is connected to.
		 */
		@Column(name = "provider_user_id")
		public String getProviderUserId() {
			return providerUserId;
		}

		@Column(name = "user_id")
		public String getUserId() {
			return userId;
		}

		public void setProviderId(String providerId) {
			this.providerId = providerId;
		}

		public void setProviderUserId(String providerUserId) {
			this.providerUserId = providerUserId;
		}
		
		public void setUserId(String userId) {
			this.userId = userId;
		}
	}

}